package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.common.Result;
import com.itheima.mapper.ArticlesMapper;
import com.itheima.mapper.BsUserMapper;
import com.itheima.pojo.BsUser;
import com.itheima.pojo.hm.HmArticles;
import com.itheima.service.ArticlesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticlesServiceImpl implements ArticlesService {

    @Autowired
    private ArticlesMapper articlesMapper;

    @Autowired
    private BsUserMapper bsUserMapper;

    //todo 面试技巧分页查询
    @Override
    public Result findPage(Integer page, Integer pagesize, String keyword, Integer state) {
        Page<HmArticles> p = new Page<>();
        LambdaQueryWrapper<HmArticles> wrapper = new LambdaQueryWrapper<>();
        //判断是否需要条件查询
        if (keyword != null){
            wrapper.like(HmArticles::getTitle,keyword);
        }
        //判断是否根据状态查询
        if (state != null){
            wrapper.eq(HmArticles::getState,state);
        }
        Page<HmArticles> ha = articlesMapper.selectPage(p, wrapper);
        //根据creatorID查询pe_permission_group表中的name
        LambdaQueryWrapper<BsUser> wrapper2 = new LambdaQueryWrapper<>();
        List<HmArticles> records = ha.getRecords();
        //遍历查询出pe_permission_group对象然后封装到HmArticles对象里
        for (HmArticles record : records) {
            wrapper2.eq(BsUser::getId,record.getCreatorID());
            BsUser bsUser = bsUserMapper.selectOne(wrapper2);
            record.setCreator(bsUser.getUsername());
        }

        return new Result(ha.getTotal(),pagesize,ha.getPages(),page,records);
    }
    //todo 添加面试技巧数据
    @Override
    public void add(HmArticles hmArticles) {
        articlesMapper.insert(hmArticles);
    }
    //todo 启用禁用状态
    @Override
    public R modifyStatus(Integer id, Integer status) {
        LambdaUpdateWrapper<HmArticles> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(HmArticles::getId,id);
        HmArticles articles = articlesMapper.selectOne(wrapper);
        articles.setState(status);
        articlesMapper.updateById(articles);
        return R.success(true);
    }
    //todo 删除
    @Override
    public R deleteById(Integer id) {
        articlesMapper.deleteById(id);
        return R.success(true);
    }
    //todo 编辑修改
    @Override
    public R modify(Integer id, HmArticles hmArticles) {
        articlesMapper.updateById(hmArticles);
        return R.success(true);
    }
    //todo 文章详情
    @Override
    public HmArticles findById(Integer id) {
        HmArticles hmArticles = articlesMapper.selectById(id);
        return hmArticles;
    }

    @Override
    public BsUser findUserId(Integer userId) {
        BsUser bsUser = bsUserMapper.selectById(userId);
        return bsUser;
    }
}
